21. CODE: Read the Board from a File
Reading the Board from a File
Until now, the board has been declared and initialized in the
main()
function. As discussed in the previous notebook, you will need a function to read the board in from another file in order to make the program a little more flexible and user-friendly.
The first step in this process will be to write a
ReadBoardFile
function that reads in the file and prints each line to
cout
. The output should look like the
1.board
file, which can be opened in the editor below:
0,1,0,0,0,0,
0,1,0,0,0,0,
0,1,0,0,0,0,
0,1,0,0,0,0,
0,0,0,0,1,0,
To Complete This Exercise:
- Write a function
void ReadBoardFile
which reads lines from a file. Write each line tocout
, followed by a newline character"\n"
. The function should accept a string argument, which represents the path to the file. For now, sinceReadBoardFile
does not need to return anything, you can use avoid
return type.- Call
ReadBoardFile
from main using the argument"1.board"
.
Note:
you will need to include the
fstream
class, and you may want to have
using std::ifstream
to use the
ifstream
object without having to write
std::
in the rest of your code.
Workspace
This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity , so you may be able to download them there.
Workspace Information:
- Default file path:
- Workspace type: generic
- Opened files (when workspace is loaded): n/a
-
userCode:
export CXX=g++-7
export CXXFLAGS=-std=c++17
g++() {
/usr/bin/g++-7 -std=c++17 "$1"
}
export -f g++